// pages/account/booking/booking.js
let common = require('../../../utils/common.js');
Page({
  data: {
    tabActive: ['tab-active', '', ''],
    activities: [],
    loadHidden: true,
    hasMore: false,
    tipNone: true,
  },
  skip: 0,
  take: 10,
  sltTypeid: 1,
  tabSwitch: function (e) {
    let that = this, idx = parseInt(e.currentTarget.dataset.index), typeid = parseInt(e.currentTarget.dataset.type), tab = ['', '', ''];
    if (typeid == that.sltTypeid) return;
    tab[idx] = 'tab-active';
    that.setData({
      tabActive: tab,
      activities: [],
    });
    that.skip = 0;
    that.take = 10;
    that.sltTypeid = typeid;
    that.reqActivity(typeid);
  },
  reqActivity: function (status) {
    let that = this;
    common.asynPost(common.API.user.activityorders, {
      skip: that.skip,
      take: that.take,
      status: status
    }, function (data) {
      that.setData({
        loadHidden: true,
      });
      if (data['orders'] && data['orders'].length) {
        that.setData({
          activities: that.data.activities.concat(data['orders']),
          tipNone: true,
        });
      } else {
        that.skip -= that.take;
        that.setData({
          hasMore: true,
          tipNone: !that.data.activities.length,
        });
      }
    }, function (err) {
      that.setData({
        loadHidden: true,
      });
      that.skip -= that.take;
    });
  },
  onReachBottom: function () {
    var that = this;
    that.setData({
      tipNone: false,
      hasMore: false
    });
    that.skip += that.take;
    that.reqActivity(that.sltTypeid);
  },
  payOrder(e) {
    let that = this, id = parseInt(e.currentTarget.dataset.id);
    common.asynPost(common.API.user.payorder, { id: id }, function (data) {
      if (data['order']) {
        var result = data['order'];
        result.time = result.startTime;
        result.cover = that.data.activities.filter(function(a){
          return a.id == id;
        })[0].cover;
        wx.redirectTo({
          url: '../../pay/bookedinfo/bookedinfo?detail=' + JSON.stringify(result)
        });
      }
    });
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      loadHidden: false
    });
    this.reqActivity(1);
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})